package cn.ainit.yz.clinStore;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ainit.common.ReturnObject;
import cn.ainit.sys.utils.UUID32;
import cn.ainit.yz.clinStore.entity.ClinStock;
import cn.ainit.yz.clinStore.entity.ClinStockItem;
import cn.ainit.yz.clinStore.entity.ClinStockObj;

@Controller
@RequestMapping("/clinStock")
public class ClinStockController {
	private ClinStockService clinStockService;
	@Resource
	public void setClinStockService(ClinStockService clinStockService) {
		this.clinStockService = clinStockService;
	}
	
	
	@RequestMapping("/saveClinStock")
	@ResponseBody
	public ReturnObject saveClinStock(@RequestBody ClinStockObj clinStockObj, HttpServletRequest request){
		ReturnObject returnObject = new ReturnObject();
		ClinStock inStock= clinStockObj.getClinStock();
		List<ClinStockItem> clinStockItems = clinStockObj.getClinStockItems();

		try {
			System.out.println("sprkId:"+inStock.getInId());
			if (inStock.getInId() != null && !inStock.getInId().equals("")) {
				//删除旧的不存在的记录    条件  sp_id 和 sp_item_id
				if(clinStockService.updClinStock(inStock, clinStockItems)){
					returnObject.setMsg("upsuccess");
				}else{
					returnObject.setMsg("upfail");
				}
				
			}else{
				String sprkId = UUID32.newGuid();
				inStock.setInId(sprkId);
				//保存操作
				if(clinStockService.saveClinStock(inStock, clinStockItems)){
					returnObject.setMsg("success");
					returnObject.setInfo(sprkId);
				}else{
					returnObject.setMsg("fail");
				}
			}
		} catch (Exception e) {
			returnObject.setMsg(e.getMessage());
			e.printStackTrace();
		}
		return returnObject;
	}
}
